package com.sumover.Service.impl;

import com.sumover.DAO.*;
import com.sumover.POJO.*;
import com.sumover.Service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private GameCommentMapper gameCommentMapper;
    @Autowired
    private ArticleCommentMapper articleCommentMapper;
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private ReplyCommentMapper replyCommentMapper;

    @Override
    public List<GameComment> getGameComments(Game game) {
        List<GameComment> gameComments = gameCommentMapper.getGameComments(game);
        for (GameComment gameComment : gameComments) {
            Comment comment = commentMapper.selectByPrimaryKey(gameComment.getCid());
            gameComment.setUid(comment.getUid());
            gameComment.setCommentdetail(comment.getCommentdetail());
            gameComment.setLevel(comment.getLevel());
        }
        return gameComments;
    }

    @Override
    public boolean haveReply(Object level) {
        if (level instanceof Comment) {
            Comment comment = (Comment) level;
            List<Reply> replies = replyMapper.selectReplyByComment(comment);
            return !replies.isEmpty();
        }
        if (level instanceof Reply) {
            Reply reply = (Reply) level;
            List<Reply> replies = replyMapper.selectReplyByReply(reply);
            return !replies.isEmpty();
        }
        return false;
    }

    @Override
    public Object getReply(Object level) {
        if (level instanceof Comment) {
            Comment comment = (Comment) level;
            List<Reply> replies = replyMapper.selectReplyByComment(comment);
            return replies;
        }
        if (level instanceof Reply) {
            Reply reply = (Reply) level;
            List<Reply> replies = replyMapper.selectReplyByReply(reply);
            return replies;
        }
        return null;
    }

    @Override
    public List<ArticleComment> getArticleComments(Article article) {
        List<ArticleComment> articleComments = articleCommentMapper.getAllComments(article);
        return articleComments;
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Level> getGameLevels(Game game) {
        List<Integer> allReplies = commentMapper.getAllGameReplies(game);
        List<Integer> allGameComments = commentMapper.getAllGameComments(game);
        List<Level> levels = new LinkedList<Level>();
        //  获取所有的回复
        for (Integer replyId : allReplies) {
            Level level = new Level();
            Reply reply = replyMapper.selectByPrimaryKey(replyId);
            level.setLevelContent(reply.getReContent());
            level.setLevelCount(reply.getLevel());
            level.setLid(replyId);
            level.setPublisher(userMapper.selectByPrimaryKey(reply.getUid()));
            level.setPublishTime(reply.getrTime());
            level.setReplyTo(reply.getReTo());
            levels.add(level);
        }
        //  获取所有的评论
        for (Integer commentId : allGameComments) {
            Level level = new Level();
            Comment comment = commentMapper.selectByPrimaryKey(commentId);
            level.setLevelContent(comment.getCommentdetail());
            level.setLevelCount(comment.getLevel());
            level.setLid(commentId);
            level.setPublisher(userMapper.selectByPrimaryKey(comment.getUid()));
            level.setPublishTime(comment.getPublishtime());
            level.setReplyTo(-1);
            levels.add(level);
        }
        Collections.sort(levels);
        return levels;
    }


    @Override
    public List<Level> getArticleLevels(Article article) {
        List<Integer> allReplies = commentMapper.getAllArticleReplies(article);
        List<Integer> allComments = commentMapper.getAllArticleComments(article);
        List<Level> levels = new LinkedList<Level>();
        for (Integer replyId : allReplies) {
            Level level = new Level();
            Reply reply = replyMapper.selectByPrimaryKey(replyId);
            level.setLevelContent(reply.getReContent());
            level.setLevelCount(reply.getLevel());
            level.setLid(replyId);
            level.setPublisher(userMapper.selectByPrimaryKey(reply.getUid()));
            level.setPublishTime(reply.getrTime());
            level.setReplyTo(reply.getReTo());
            levels.add(level);
        }
        for (Integer commentId : allComments) {
            Level level = new Level();
            Comment comment = commentMapper.selectByPrimaryKey(commentId);
            level.setLevelContent(comment.getCommentdetail());
            level.setLevelCount(comment.getLevel());
            level.setLid(commentId);
            level.setPublisher(userMapper.selectByPrimaryKey(comment.getUid()));
            level.setPublishTime(comment.getPublishtime());
            level.setReplyTo(-1);
            levels.add(level);
        }
        Collections.sort(levels);
        return levels;
    }

    /**
     * 过程:
     * 1.  在tb_comment中插入
     * 2.  在tb_game_comment中插入
     *
     * @param game
     * @param commentText
     * @param user
     */
    @Override
    public void addNewGameComments(Game game, String commentText, User user) {
        commentMapper.addNewComment(user, commentText, this.getGameLevels(game).size());
        Integer cid = commentMapper.getMaxCommentId();
        commentMapper.addGameComment(cid, game.getGid());
    }

    @Override
    public void addNewArticleComment(Article article, String commentText, User loginUser) {
        commentMapper.addNewComment(loginUser, commentText, this.getArticleLevels(article).size());
        Integer cid = commentMapper.getMaxCommentId();
        commentMapper.addArticleComment(cid, article.getArticleid());
    }

    /**
     * 添加一个文章的回复.
     * 1.  在tb_reply中添加
     * 2.  在tb_article_replies中添加
     *
     * @param article
     * @param replyToLevel
     * @param replyText
     * @param loginUser
     */
    @Override
    public void addNewReplyInArticle(Article article, Integer replyToLevel, String replyText, User loginUser) {
        replyMapper.addNewReply(replyToLevel, replyText, this.getArticleLevels(article).size(), loginUser);
        Integer rid = replyMapper.getMaxReplyId();
        commentMapper.addArticleReply(rid, article.getArticleid());
    }

    /**
     * 添加一个游戏的回复
     * 1.   在tb_reply中添加
     * 2.   在tb_game_replies中添加
     *
     * @param game
     * @param replyToLevel
     * @param replyText
     * @param loginUser
     */
    @Override
    public void addNewReplyInGame(Game game, Integer replyToLevel, String replyText, User loginUser) {
        replyMapper.addNewReply(replyToLevel, replyText, this.getGameLevels(game).size(), loginUser);
        Integer rid = replyMapper.getMaxReplyId();
        commentMapper.addGameReply(rid, game.getGid());
    }


}
